package system.commodityManagement.impl;

import system.commodityManagement.CommodityManagementInterface;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;

/**
 * Created by 陈文杰 on 2022/4/13 20:56
 */
@SuppressWarnings("all")
public class CommodityManagementImpl implements CommodityManagementInterface {


    //保存商品对象
    private Map<Integer, Commodity> commodityMap = new HashMap<>();
    //保存商品名对应的商品id
    private Map<String, Integer> commodityNameMap = new HashMap<>();
    volatile private int id = 0;
    private Commodity commodity;
    private Scanner scanner = new Scanner(System.in);

    public CommodityManagementImpl() {
        initialize("老坛酸菜",9.9,20);
        initialize("光头牌拖鞋",19.9,20);
        initialize("熊大牌肥皂",19.9,20);
        initialize("广东麻将",56,40);
        initialize("李丁牌球鞋",45,40);
        initialize("奶次牌耳机",78,40);
    }

    private void initialize(String name,double price,int count){
        commodity =new Commodity();
        commodity.setId(++id);
        commodity.setCount(count);
        commodity.setName(name);
        commodity.setPrice(price);
        commodityMap.put(id,commodity);
        commodityNameMap.put(name,id);
    }
    //1.商品入库方法
    @Override
    public boolean commodityStorage(String name, int count) {
        //不存在该商品
        if (commodityNameMap.get(name) == null) {
            System.out.print("*库存中不存在[" + name + "],是否增添该商品[y or else]:");
            if (scanner.next().equals("y")) {
                System.out.print("*请告诉我此商品的价格:");
                double price = scanner.nextDouble();
                newlyCommodity(name, count, price);
                return true;
            }
            {
                System.out.println("\t***已退出该功能***\n");
                return false;
            }
        }
        //存在该商品
        {
            int commodityId = commodityNameMap.get(name);
            commodity = commodityMap.get(commodityId);
            int newCount = commodity.getCount() + count;
            commodity.setCount(newCount);
            commodityMap.put(commodityId, commodity);
            System.out.println("*商品[" + name + "]已添加[" + count + "]\t总数为[" + newCount + "]*\n");
            return true;
        }
    }

    //2.商品出库方法,检索商品名
    @Override
    public boolean commodityRemoval(String name, int count) {
        //存在该商品
        if (commodityNameMap.containsKey(name)) {
            int commodityId = commodityNameMap.get(name);
            commodity = commodityMap.get(commodityId);
            int commodityNum = commodity.getCount();

            if (commodityNum >= count) {//库存数不少于出库数
                commodityNum -= count;
                if (commodityNum == 0) {//库存数等于出库数
                    System.out.println("[" + name + "]刚好卖完啦!赶紧进货喔");
                }
                commodity.setCount(commodityNum);
                commodityMap.put(commodityId, commodity);
                return true;
            }
            if (commodityNum == 0) {//存在该商品,但库存数为零
                System.out.println("*早就卖光啦!赶紧进货");
                return false;
            }
            if (commodityNum > 0) {//存在该商品,但库存数少于出库数
                System.out.println("[" + name + "]只有[" + commodityNum + "],不足以出库");
                return false;
            }
            System.out.println("失败");
            return false;
        }
        //不存在此商品
        {
            System.out.println("暂无此商品!看下商品名是不是打错啦~");
            return false;
        }
    }

    //商品出库方法,检索商品号
    @Override
    public boolean commodityRemoval(int id, int count) {
        //存在该商品
        if (commodityMap.containsKey(id)) {
            commodity = commodityMap.get(id);
            int commodityNum = commodity.getCount();

            if (commodityNum >= count) {//库存数不少于出库数
                commodityNum -= count;
                if (commodityNum == 0) {//库存数等于出库数
                    System.out.println("[" + commodity.getName() + "]刚好卖完啦!赶紧进货喔");
                }
                commodity.setCount(commodityNum);
                commodityMap.put(id, commodity);
                return true;
            }
            if (commodityNum == 0) {//存在该商品,但库存数为零
                System.out.println("*早就卖光啦!赶紧进货");
                return false;
            }
            if (commodityNum > 0) {//存在该商品,但库存数少于出库数
                System.out.println("[" + commodity.getName() + "]只有[" + commodityNum + "],不足以出库");
                return false;
            }
            System.out.println("失败");
            return false;
        }
        //不存在此商品
        {
            System.out.println("暂无此商品!看下商品编号是不是打错啦~");
            return false;
        }
    }

    //3.新增商品方法
    @Override
    public boolean newlyCommodity(String name, int count, double price) {
        //不存在此商品,可以新增
        if (!commodityNameMap.containsKey(name)){
            commodity = new Commodity();
            commodity.setId(++id);
            commodity.setCount(count);
            commodity.setName(name);
            commodity.setPrice(price);
            commodityMap.put(id, commodity);
            commodityNameMap.put(name, id);
            System.out.println("商品[" + name + "]已添入库存中\t总数为[" + count + "]\t价格为[" + price + "]\n");
            return true;
        }
        //库存中存在此商品,调用入库方法
        {
            commodityStorage(name, count);
            System.out.println("商品[" + name + "]库存中已经有啦,已自动添入\n");
            return true;
        }
    }

    //4.查询全部商品方法
    @Override
    public void inquireCommodityAll() {
        Iterator iterator = commodityMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<Integer, Commodity> entry = (Map.Entry<Integer, Commodity>) iterator.next();
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(entry.getValue().toString());
        }
        System.out.println("<已检索所有商品>");
        try {
            Thread.sleep(700);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    //5.按编号查询商品
    @Override
    public void inquireCommodityAccordingNum(Integer id) {
        //商品存在
        if (commodityMap.containsKey(id)){
            System.out.println(commodityMap.get(id).toString());
            System.out.println("<已检索到商品>");
            return;
        }
        //商品不存在
        {
            System.out.println("<未检索到商品>");
        }
        try {
            Thread.sleep(700);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    //6.展示所有商品
    public void showCommodityAll(){
        Iterator iterator = commodityMap.entrySet().iterator();
        int i=0;//计数器
        System.out.println("******************************************************************");
        while (iterator.hasNext()) {
            Map.Entry<Integer, Commodity> entry = (Map.Entry<Integer, Commodity>) iterator.next();
            System.out.print("|| "+entry.getValue().printCommodity()+"\t||");
            if (i++==2){
                i=0;
                System.out.println();
            }
        }
        System.out.println("\n******************************************************************");
    }

    public Map<Integer, Commodity> getCommodityMap() {
        return commodityMap;
    }
}
